package com.zeyi.chat.service;

import com.zeyi.chat.mapper.GroupMapper;
import com.zeyi.chat.mapper.UserMapper;
import com.zeyi.chat.po.ChatGroup;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author zeyi
 */
public class GroupService {
    private GroupService() {
    }

    private static class Singleton {
        public static final GroupService INSTANCE = new GroupService();
    }

    public static GroupService getInstance() {
        return GroupService.Singleton.INSTANCE;
    }

    /**
     * 通过ID查找群
     */
    public ChatGroup getGroupById(Integer groupId) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        ChatGroup group = groupMapper.selectByPrimaryKey(groupId);
        session.close();
        return group;
    }

    /**
     * 删除群成员
     */
    public void deleteMember(Integer groupId, Integer memberId) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        groupMapper.deleteMember(groupId, memberId);
        session.commit();
        session.close();
    }

    /**
     * 更新群信息
     */
    public void updateInfo(ChatGroup group) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        groupMapper.updateByPrimaryKeySelective(group);
        session.commit();
        session.close();
    }

    /**
     * 通过群号查找群
     */
    public ChatGroup findByGroupCode(String groupCode) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        ChatGroup group = groupMapper.findByGroupCode(groupCode);
        session.close();
        return group;
    }

    /**
     * 查找群成员
     */
    public List<User> getGroupMember(Integer groupId) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        List<User> groupMember = groupMapper.getGroupMembers(groupId);
        session.close();
        return groupMember;
    }

    /**
     * 创建群
     */
    public void createGroup(ChatGroup group) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        groupMapper.insert(group);
        groupMapper.addGroupMember(group.getId(), group.getCreateId());
        session.commit();
        session.close();
    }

    /**
     * 加入群
     */
    public ChatGroup joinGroup(Integer userId, Integer groupId) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        try {
            groupMapper.addGroupMember(groupId, userId);
            session.commit();
            return groupMapper.selectByPrimaryKey(groupId);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 删除群
     */
    public void deleteById(Integer groupId) {
        SqlSession session = MybatisUtil.getSession();
        GroupMapper groupMapper = session.getMapper(GroupMapper.class);
        groupMapper.deleteByPrimaryKey(groupId);
        session.commit();
        session.close();
    }
}
